package cn.study.controller;


import cn.study.entity.StudyEmployee;
import cn.study.service.StudyEmployeeService;
import cn.study.service.StudyRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @date 2020/6/23 -15:26
 */

@Controller
public class LoginController {
    @Autowired
    StudyEmployeeService studyEmployeeService;
    @Autowired
    StudyRoleService studyRoleService;

    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        Model model, HttpSession session
    ) {
        HashMap<String, Object> map = new HashMap<>();
        // 自定义要查询
        map.put("user_name", username);
        map.put("password", password);
        StudyEmployee studyEmployee = studyEmployeeService.login(map);
        if(studyEmployee==null){
            model.addAttribute("msg","账号或密码错误");
            return "login";
        }else if(studyEmployee.getEmpState()!=1){
            String str=studyEmployee.getEmpState()==2?"你已离职":"你已被辞退🤬";
            model.addAttribute("msg",str);
            return "login";
        }

        session.setAttribute("USER",studyEmployee);
        return "redirect:/study/index";
    }
    @GetMapping("/exit")
    public String exit(HttpServletRequest request){
        request.getSession().removeAttribute("USER"); //遍历删除session中的值
        return "login";
    }
}